// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.process;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.model.warehouse.ProcessType;
import org.dengues.model.warehouse.WarehouseFactory;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.wizard.Wizard;

/**
 * qzhang class global comment. Detailled comment <br/>
 * 
 */
public class NewProcessWizard extends Wizard {

    private NewProcessWizardPage page;

    private IPath path;

    private ProcessType process;

    private boolean isActived;

    /**
     * qzhang NewProcessWizard constructor comment.
     */
    public NewProcessWizard(IPath path) {
        this();
        this.path = path;
        setWindowTitle(Messages.getString("NewProcessWizard.Title")); //$NON-NLS-1$
    }

    /**
     * qzhang NewProcessWizard constructor comment.
     */
    private NewProcessWizard() {
        super();
        setDefaultPageImageDescriptor(ImageUtil.getDescriptor(IDenguesSharedImage.WAREHOUSE_WIZARD_PROCESS));
    }

    /**
     * qzhang NewProcessWizard constructor comment.
     */
    public NewProcessWizard(ProcessType process, IPath path, boolean isActived) {
        this();
        this.process = process;
        this.path = path;
        this.isActived = isActived;
        setWindowTitle(Messages.getString("NewProcessWizard.editProperties")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages() {
        if (process != null) {
            page = new NewProcessWizardPage(process, isActived);
        } else {
            page = new NewProcessWizardPage(path);
        }
        addPage(page);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#performFinish()
     */
    @Override
    public boolean performFinish() {
        boolean create = false;
        boolean rename = false;
        String processName = page.getProcessName();
        if (process == null) {
            process = WarehouseFactory.eINSTANCE.createProcessType();
            create = true;
        } else if (path != null) {
            if (!process.getName().equals(processName)) {
                rename = true;
                WarehouseResourceFactory.deleteObjectDataPhysical(process);
            }
        }
        process.setComment(page.getProcessComment());
        process.setName(processName);
        process.setStatus(page.getProcessStatus());
        if (create || rename) {
            WarehouseResourceFactory.create(process, path);
        } else {
            WarehouseResourceFactory.save(process);
        }
        return true;
    }

    public ProcessType getProcess() {
        return process;
    }

}
